package com.example.wangbo.startproject_310wxy.model;





import com.example.wangbo.startproject_310wxy.bean.WeatherResult;
import com.example.wangbo.startproject_310wxy.iface.WeatherIface;
import com.example.wangbo.startproject_310wxy.iface.WeatherListener;
import com.example.wangbo.startproject_310wxy.serive.Weather51Service;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by BOT-CC on 2017/3/14.
 */

public class Weather51Model implements WeatherIface {
    private Retrofit retrofit;
    private String BASEURL="http://weather.51wnl.com/weatherinfo/";
    public Weather51Model() {
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();//主机地址
    }


    public void getWeather(String cityno, final WeatherListener listener) {
        //Retrofit接口使用3--2
        Weather51Service weather51Service = retrofit.create(Weather51Service.class);
        Call<WeatherResult> call = weather51Service.getResult(cityno,1);
        //Retrofit接口使用3-3
        call.enqueue(new Callback<WeatherResult>() {

            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                System.out.println("----333333");
                if(response.isSuccessful() && response.body() != null)
                    listener.onResponse(response.body().getWeatherinfo());
                else
                    listener.onFail("onResponse error");
            }


            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
